package com.university.report.common.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @ClassName health_report
 * @Author Jinondo
 * @Date 2021/6/18 9:46
 */
@Configuration
public class RedisMultiConfig {

    @Value("${spring.redis.db2.host:null}")
    private String secondHost;
    @Value("${spring.redis.db2.port:0}")
    private Integer secondPort;
    @Value("${spring.redis.db2.database:0}")
    private Integer secondDatabase;

    @Value("${spring.redis.db3.host:null}")
    private String thirdHost;
    @Value("${spring.redis.db3.port:0}")
    private Integer thirdPort;
    @Value("${spring.redis.db3.database:0}")
    private Integer thirdDatabase;


    //最大空闲连接数
    private static final int MAX_IDLE = 200;
    // 最小空闲连接数
    private static final int MIN_IDLE = 0;
    //最大连接数
    private static final int MAX_TOTAL = 1024;
    //建立连接最长等待时间
    private static final long MAX_WAIT_MILLIS = 10000;


    /**
     * 创建redis连接工厂
     */
    public JedisConnectionFactory createJedisConnectionFactory(int dbIndex, String host, int port) {

        RedisStandaloneConfiguration redisStandaloneConfiguration =
                new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(dbIndex);
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
//        redisStandaloneConfiguration.setPassword(password);
        // 获得默认的连接池构造器
        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb =
                (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();
        //指定 jedisPoolConfig 来修改默认的连接池构造器
        jpcb.poolConfig(poolConfig(MAX_IDLE,MIN_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, true));
        //通过构造器来构造jedis客户端配置
        JedisClientConfiguration jedisClientConfiguration = jpcb.build();
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;

    }

    /**
     * 连接池配置
     * @param maxIdle
     * @param minIdle
     * @param maxTotal
     * @param maxWaitMillis
     * @param testOnBorrow
     * @return
     */
    public JedisPoolConfig poolConfig(int maxIdle, int minIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxWaitMillis(maxWaitMillis);
        poolConfig.setTestOnBorrow(testOnBorrow);
        return poolConfig;
    }

    @Bean(name = "redisTemplate2")
    public StringRedisTemplate redisTemplate2() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(
                createJedisConnectionFactory(secondDatabase, secondHost, secondPort));
        template.afterPropertiesSet();
        return template;
    }

    @Bean(name = "redisTemplate3")
    public StringRedisTemplate redisTemplate3() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(
                createJedisConnectionFactory(thirdDatabase, thirdHost, thirdPort));
        template.afterPropertiesSet();
        return template;
    }

    // 自己定义了一个 RedisTemplate
//    @Bean(name = "redisTemplate3")
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory
                                                               factory) {
        // 我们为了自己开发方便，一般直接使用 <String, Object>
        RedisTemplate<String, Object> template = new RedisTemplate<String,
                Object>();
        template.setConnectionFactory(factory);
        // Json序列化配置
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new
                Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // String 的序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
